package com.atguigu.gmall.realtime.utils

import redis.clients.jedis.{Jedis, JedisPool, JedisPoolConfig}

//其是伴生对像，在伴生对象里声明的属性都是单例的，所以指挥被初始化一次
object MyRedisUtils {
  def main(args: Array[String]): Unit = {
    val jedis: Jedis = get()
    println(jedis)
    close(jedis)

  }


  def close(jedis: Jedis ): Unit ={
    if(jedis != null ) jedis.close()
  }
  def get(): Jedis={
    jedisPool.getResource
  }
  def ceateJidesPool() : JedisPool={
    val redisHost = MyPropsUtil(MyConfig.REDIS_HOST)
    val redisPort = MyPropsUtil(MyConfig.REDIS_PORT)
    val jedisPoolConfig: JedisPoolConfig = new JedisPoolConfig()
    jedisPoolConfig.setMaxTotal(100) //最大连接数
    jedisPoolConfig.setMaxIdle(20) //最大空闲
    jedisPoolConfig.setMinIdle(20) //最小空闲
    jedisPoolConfig.setBlockWhenExhausted(true) //忙碌时是否等待
    jedisPoolConfig.setMaxWaitMillis(5000) //忙碌时等待时长 毫秒
    jedisPoolConfig.setTestOnBorrow(true) //每次获得连接的进行测试
    val pool: JedisPool = new JedisPool(jedisPoolConfig,redisHost)
    pool
  }

  private val jedisPool = ceateJidesPool()
}
